package com.wude.guild.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wude.bean.StartFuBean;
import com.wude.guild.R;
import com.wude.guild.adapter.GameDetListQufuAdapter;
import com.wude.guild.manager.DownloadManager;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 游戏详情开服页面Fragment
 * Created by Administrator on 2017/3/24.
 */

public class GameDetKaifuFragment extends Fragment {

    @BindView(R.id.error_text)
    TextView errorText;
    @BindView(R.id.error_layout)
    RelativeLayout errorLayout;
    @BindView(R.id.game_listview)
    ListView gameListview;
    private GameDetListQufuAdapter gameDetListQufuAdapter;
    private String id;
    ArrayList<StartFuBean> gameInfos = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_gamedet_kaifu, null);
        ButterKnife.bind(this, inflate);
        id = getArguments().getString("key");
        gameDetListQufuAdapter = new GameDetListQufuAdapter(getActivity());
        gameListview.setAdapter(gameDetListQufuAdapter);
        initdata();
        return inflate;
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("GameDetKaifuFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("GameDetKaifuFragment");
    }

    private void initdata() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("game_id", id);
        HttpCom.POST(handler, HttpCom.getGameDetKaifuUrl(), map, false);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    ArrayList<StartFuBean> startFuBeen = HttpUtils.DNSQUfu(msg.obj.toString());
                    if (startFuBeen != null) {
                        gameListview.setVisibility(View.VISIBLE);
                        errorLayout.setVisibility(View.GONE);
                        gameInfos.addAll(startFuBeen);
                        gameDetListQufuAdapter.setList(gameInfos);
                    } else {
                        gameListview.setVisibility(View.GONE);
                        errorLayout.setVisibility(View.VISIBLE);
                        errorText.setText("暂无开服信息");
                    }
                    break;
                case 2:
                    gameListview.setVisibility(View.GONE);
                    errorLayout.setVisibility(View.VISIBLE);
                    errorText.setText("网络异常");
                    break;
            }
        }
    };
}
